//
//  LSZBranchRankingViewController.m
//  PartyClassCloud
//
//  Created by 梁素珍 on 2018/9/2.
//  Copyright © 2018年 九酷. All rights reserved.
//

#import "LSZBranchRankingViewController.h"

@interface LSZBranchRankingViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong) UITableView * tableViewBranchRanking;
@property(nonatomic,strong) LSZBranchPointsManager * branchPointsManager;
@end

@implementation LSZBranchRankingViewController
-(void)intWithBranchRanking
{
    [LSZRequestDicData initWithBranchPointsRanking:^(BOOL isOk, NSString * string, LSZBranchPointsManager * manager) {
        if (isOk == YES) {
            self.branchPointsManager = manager;
            [self.tableViewBranchRanking reloadData];
        }else{
            if ([string isEqualToString:@"重复登录"]) {
                [LSZPublicClassDataToll RepeatLoginInterfaceJump:self.view fromVC:@"LSZBranchRankingViewController"];
            }else{
                [LSZPublicClassDataToll initWithAlertKuangView:self.view string:string];
            }
        }
    }];
}
-(void)goToLogin
{
    [LSZPublicClassDataToll goToLogin];
}
-(void)addNotificationCenter
{
    NSString * strDelete = [NSString stringWithFormat:@"%@Login",@"LSZBranchRankingViewController"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(goToLogin) name:strDelete object:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addNotificationCenter];
    [self intWithBranchRanking];
    [self.tableViewBranchRanking registerClass:[LSZMyScoreTableViewCell class] forCellReuseIdentifier:@"LSZMyScoreTableViewCell"];
    [self.tableViewBranchRanking registerClass:[LSZNameDepartmentTableViewCell class] forCellReuseIdentifier:@"LSZNameDepartmentTableViewCell"];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.branchPointsManager.arrayData.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 150;
    }
    return 80;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 40;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        LSZMyScoreTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZMyScoreTableViewCell" forIndexPath:indexPath];
        for (int i = 0; i<self.branchPointsManager.arrayData.count; i++) {
            LSZBranchPointsMg * mg = nil;
            mg = self.branchPointsManager.arrayData[i];
            if ([[_MyCenter branch_name] isEqualToString:mg.name]) {
                cell.labelRanking.text = [NSString stringWithFormat:@"我的排名:%ld",(long)i+1];
                break;
            }
        }
        cell.labelNumber.text = [NSString stringWithFormat:@"我的得分:%@",[_MyCenter departmentScore]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        LSZNameDepartmentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZNameDepartmentTableViewCell" forIndexPath:indexPath];
        cell.labelNumber.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
//        branchPointsManager
        LSZBranchPointsMg * mg = nil;
        mg = self.branchPointsManager.arrayData[indexPath.row-1];
        cell.labelName.text = mg.name;
        cell.labelDepartment.text = [NSString stringWithFormat:@"%@分",mg.score];
        if (indexPath.row < 4) {
            cell.imageViewIcon.hidden = NO;
            if(indexPath.row == 1 ){
                cell.imageViewIcon.image = [UIImage imageNamed:@"第一名"];
            }else if (indexPath.row == 2){
                cell.imageViewIcon.image = [UIImage imageNamed:@"第二名"];
            }
            else{
                cell.imageViewIcon.image = [UIImage imageNamed:@"第三名"];
            }
        }else{
            cell.imageViewIcon.hidden = YES;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
-(UITableView *)tableViewBranchRanking
{
    if (_tableViewBranchRanking == nil) {
        _tableViewBranchRanking = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-44-22) style:UITableViewStyleGrouped];
        _tableViewBranchRanking.delegate = self;
        _tableViewBranchRanking.dataSource = self;
        _tableViewBranchRanking.estimatedRowHeight=0;
        _tableViewBranchRanking.estimatedSectionFooterHeight =0;
        _tableViewBranchRanking.estimatedSectionHeaderHeight =0;
        [_tableViewBranchRanking setSeparatorInset:UIEdgeInsetsMake(0, 20, 0, 20)];
        [self.view addSubview:_tableViewBranchRanking];
    }
    return _tableViewBranchRanking;
}

@end
